package com.ssy.lingxi.product.controller.pc.customer;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CustomerAttributeValueRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.request.SimpleStatusRequest;
import com.ssy.lingxi.product.api.model.response.CustomerAttributeValueResponse;
import com.ssy.lingxi.product.entity.customer.CustomerAttributeValue;
import com.ssy.lingxi.product.service.customer.ICustomerAttributeValueService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 商品能力--属性值管理
 * 商品能力--属性值管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/customer")
public class CustomerAttributeValueController extends BaseController {
    
    @Resource
    private ICustomerAttributeValueService customerAttributeValueService;

    /**
     * 同步平台后台属性
     * @param simpleIdListRequest 平台后台属性值id集合
     * @return
     */
    @RequestMapping(value = "/syncAttributeValue", method = RequestMethod.POST)
    public Wrapper<Boolean> syncAttributeValue(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeValueService.syncAttributeValue(sysUser, simpleIdListRequest));
    }

    /**
     * 查询属性值列表
     * @param pageVO 分页实体
     * @param customerAttributeId 属性名id
     * @param name 属性值名称
     * @return Wrapper<PageData<CustomerAttributeValue>>
     */
    @RequestMapping(value = "/getCustomerAttributeValueList", method = RequestMethod.GET)
    public Wrapper<PageData<CustomerAttributeValueResponse>> getCustomerAttributeValueList(PageVO pageVO, @RequestParam("customerAttributeId") Long customerAttributeId, @RequestParam(value = "name", required = false) String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<CustomerAttributeValue> result = customerAttributeValueService.getCustomerAttributeValueList(sysUser, pageVO, customerAttributeId, name == null ? "" : name);
        List<CustomerAttributeValueResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CustomerAttributeValueResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询属性值信息
     * @param id 属性值id
     * @return Wrapper<CustomerAttributeValue>
     */
    @RequestMapping(value = "/getCustomerAttributeValue", method = RequestMethod.GET)
    public Wrapper<CustomerAttributeValueResponse> getCustomerAttributeValue(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        CustomerAttributeValue customerAttributeValue = customerAttributeValueService.getCustomerAttributeValue(sysUser, id);
        if(customerAttributeValue != null){
            return Wrapper.success(this.modelMapper.map(customerAttributeValue, CustomerAttributeValueResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 新增/修改属性值
     * @param customerAttributeValueRequest 参数
     * @return 属性值id
     */
    @RequestMapping(value = "/saveOrUpdateCustomerAttributeValue", method = RequestMethod.POST)
    public Wrapper<Long> saveOrUpdateCustomerAttributeValue(@RequestBody CustomerAttributeValueRequest customerAttributeValueRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeValueService.saveOrUpdateCustomerAttributeValue(sysUser, customerAttributeValueRequest));
    }

    /**
     * 删除属性值
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/deleteCustomerAttributeValue", method = RequestMethod.POST)
    public Wrapper<String> deleteCustomerAttributeValue(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeValueService.deleteCustomerAttributeValue(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 启用/停用属性值
     * @param simpleStatusRequest
     * @return
     * @throws
     */
    @RequestMapping(value = "/updateCustomerAttributeValueStatus", method = RequestMethod.POST)
    public Wrapper<String> updateCustomerAttributeValueStatus(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeValueService.updateCustomerAttributeValueStatus(sysUser, simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }
}
